package com.cas.job;

import com.cas.business.domain.BizDevice;
import com.cas.business.service.IBizDeviceService;
import com.cas.domain.MetData;
import com.cas.domain.factor.FactorMinuteData;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.bson.Document;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * 气象仪器数据处理
 */
@Component
public class MetJob {

    @Resource
    private IBizDeviceService bizDeviceService;

    @Resource
    private MongoTemplate mongoTemplate;


    @XxlJob("minuteDataJobHandler")
    public void minuteDataJobHandler() {
        // 获取所有气象仪的mn号
        List<BizDevice> bizDeviceList = bizDeviceService.selectBizDeviceList(new BizDevice());
        for (BizDevice bizDevice : bizDeviceList) {
            MetData<FactorMinuteData> metData = new MetData<>();

            Query query = new Query();
            query.addCriteria(Criteria.where("mn").is(bizDevice.getDeviceCode()));
            query.with(Sort.by(Sort.Order.desc("dataTime")));
            // 从ST22CN2011获取最新的一条数据
            Document document = mongoTemplate.findOne(query, Document.class, "ST22_CN2011");
            System.out.println(document);

            if (document != null) {
                metData.setMn(bizDevice.getDeviceCode());
                metData.setDataTime(document.getDate("dataTime").toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());

                // 从a01001获取温度数据
                if(document.get("a01001") != null) {
                    Document a01001 = (Document) document.get("a01001");
                    FactorMinuteData factorMinuteData = new FactorMinuteData();
                    factorMinuteData.setValue(new BigDecimal(a01001.getString("Rtd")));
                    factorMinuteData.setFlag(a01001.getString("Flag"));
                    factorMinuteData.initOriginalValue();
                    metData.setTemperature(factorMinuteData);
                }

                // 从a01002获取湿度数据
                if(document.get("a01002") != null) {
                    Document a01002 = (Document) document.get("a01002");
                    FactorMinuteData factorMinuteData = new FactorMinuteData();
                    factorMinuteData.setValue(new BigDecimal(a01002.getString("Rtd")));
                    factorMinuteData.setFlag(a01002.getString("Flag"));
                    factorMinuteData.initOriginalValue();
                    metData.setHumidity(factorMinuteData);
                }

                // 从a01006获取气压数据
                if(document.get("a01006") != null) {
                    Document a01006 = (Document) document.get("a01006");
                    FactorMinuteData factorMinuteData = new FactorMinuteData();
                    factorMinuteData.setValue(new BigDecimal(a01006.getString("Rtd")));
                    factorMinuteData.setFlag(a01006.getString("Flag"));
                    factorMinuteData.initOriginalValue();
                    metData.setPressure(factorMinuteData);
                }

                // 从a01007获取风速数据
                if(document.get("a01007") != null) {
                    Document a01007 = (Document) document.get("a01007");
                    FactorMinuteData factorMinuteData = new FactorMinuteData();
                    factorMinuteData.setValue(new BigDecimal(a01007.getString("Rtd")));
                    factorMinuteData.setFlag(a01007.getString("Flag"));
                    factorMinuteData.initOriginalValue();
                    metData.setWindSpeed(factorMinuteData);
                }

                // 从a01008获取风向数据
                if(document.get("a01008") != null) {
                    Document a01008 = (Document) document.get("a01008");
                    FactorMinuteData factorMinuteData = new FactorMinuteData();
                    factorMinuteData.setValue(new BigDecimal(a01008.getString("Rtd")));
                    factorMinuteData.setFlag(a01008.getString("Flag"));
                    factorMinuteData.initOriginalValue();
                    metData.setWindDirection(factorMinuteData);
                }

                // upsert数据
                Query metQuery = new Query();
                metQuery.addCriteria(Criteria.where("mn").is(bizDevice.getDeviceCode()).and("dataTime").is(metData.getDataTime()));

                Document metDocument = new Document();
                mongoTemplate.getConverter().write(metData, metDocument);
                Update update = Update.fromDocument(document);
                mongoTemplate.upsert(metQuery, update, MetData.class, "met_minute_data");
            }
        }
    }
}
